package com.bilibili.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bilibili.dubbo.api.db.UserApi;
import com.bilibili.dubbo.api.mapper.UserMapper;
import com.bilibili.model.db.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author HuangRongFu
 * 2021/11/16 星期二2:08 下午
 */

@DubboService
public class UserApiImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;


    /**
     * 通过电话查询用户信息
     *
     * @params: phone
     * @Return: User
     */
    @Override
    public User findByPhone(String phone) {

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", phone);
        return userMapper.selectOne(wrapper);
    }

    /**
     * 新增用户
     *
     * @params: user
     * @Return: Long
     */
    @Override
    public Long add(User loginUser) {

        //通过MyMetaObjectHandler 自动添加  简化代码
        //loginUser.setCreated(new Date());
        //loginUser.setUpdated(new Date());
        userMapper.insert(loginUser);

        return loginUser.getId();
    }

    /**
     * 更新用户信息
     *
     * @param: loginUser
     * @Return: void
     */
    @Override
    public void update(User loginUser) {

        userMapper.updateById(loginUser);
    }


    /**
     * 通过id范围查询用户
     *
     * @Return: List<User>
     */
    @Override
    public List<User> findUsers() {

        QueryWrapper<User> qw = new QueryWrapper<>();

        qw.between("id", 1, 50);

        return userMapper.selectList(qw);
    }

    /**
     * 通过用户id 查询用户
     *
     * @param: userId
     * @Return: User
     */
    @Override
    public User findById(Long userId) {

        return userMapper.selectById(userId);
    }

    /**
     * 根据环信id查询用户信息
     *
     * @param: huanXinId
     * @Return: ResponseEntity
     */
    @Override
    public User findByHuanXin(String huanXinId) {

        QueryWrapper<User> qw = new QueryWrapper<>();

        qw.eq("hx_user", huanXinId);

        return userMapper.selectOne(qw);
    }

}
